<?php

/**
 * Created by PhpStorm.
 * User: 周国强
 * Date: 2016/4/28
 * Time: 3:00
 */
namespace Common\Model;
use Think\Model;
class CommonModel extends Model
{
    /*
     * 删除表
     * */
    final public function drop_table($tableName){
        $tableName = C('DB_PREFIX').$tableName;
        return $this->query("drop table {$tableName}");
    }
    /*
     * 读取全部表名
     * */
    final public function list_tables(){
        $tables = array();
        $data = $this->query("show tables");
        foreach($data as $k=>$v){
            $tables[]=$v['tables_in_' . strtolower(C("DB_NAME"))];
        }
        return $tables;
    }
    /*
     * 检查表是否存在
     * */
    final public function table_exists($table) {
        $tables = $this->list_tables();
        return in_array(C("DB_PREFIX") . $table, $tables) ? true : false;
    }
    /**
     * 获取表字段
     * $table 不带表前缀
     */
    final public function get_fields($table) {
        $fields = array();
        $table = C("DB_PREFIX") . $table;
        $data = $this->query("SHOW COLUMNS FROM $table");
        foreach ($data as $v) {
            $fields[$v['Field']] = $v['Type'];
        }
        return $fields;
    }
    /**
     * 检查字段是否存在
     * $table 不带表前缀
     */
    final public function field_exists($table, $field) {
        $fields = $this->get_fields($table);
        return array_key_exists($field, $fields);
    }
    //写入数据前的回调方法 包括新增和更新
    protected function _before_write(&$data) {

    }
}